package com.alex.j2se.newfeature8;

import java.util.Arrays;
import java.util.List;

/**
 * Created by gaojun on 2017/12/2952.
 */
public class LambdaTest {

    public static void main(String[] args) {
        newThread();
        System.out.println("----------");
        forEach();
        System.out.println("----------");
        streamMap();
        System.out.println("----------");
        streamMapAndReduce();
    }

    public static void newThread() {
        new Thread(
                () -> System.out.println("hello world")
        ).start();
    }

    public static void forEach() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        list.forEach(n -> System.out.println(n));

        list.forEach(System.out::println);
    }

    public static void streamMap() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        list.stream().map((x) -> x*x).forEach(System.out::println);
    }

    public static void streamMapAndReduce() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        int sum = list.stream().map(x -> x*x).reduce((x,y) -> x + y).get();
        System.out.println(sum);
    }
}

